package com.timi_licai.timi_licai_account.fragment;


import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.timi_licai.timi_licai_account.MyConstants;
import com.timi_licai.timi_licai_account.R;
import com.timi_licai.timi_licai_account.database.DatabaseUtils;
import com.timi_licai.timi_licai_account.dialog.EditMoneyDialog;
import com.timi_licai.timi_licai_account.listener.EditBudgeInterface;
import com.timi_licai.timi_licai_account.view.WaterWaveView;

/**
 * A simple {@link Fragment} subclass.
 * 预算设置界面
 */
public class SettingFragment extends Fragment implements View.OnClickListener {

    private TextView changeSet;
    private WaterWaveView waveView;
    private int[] colors = new int[] {
            0xFFFFFF01, 0xFFFF9933, 0xFF9967CC, 0xFF00CCCC,
            0xFF00CC33, 0xFF0066CC, 0xFFFF6799, 0xFFFF83FA,
            0xFF99FF01, 0xFFFF67FF, 0xFF4876FF, 0xFFFF00FF};
    private TextView textView;
    private float sumFee = 0.0f;
    private float budge = 0.0f;
    private float result;
    private TextView txtBudge;
    private TextView txtAccount;

    public SettingFragment () {
        // Required empty public constructor
    }

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSelectedBook();
        getEditMoney();
    }

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_setting, container, false);

        waveView = (WaterWaveView) ret.findViewById(R.id.waterView);
        changeSet = (TextView) ret.findViewById(R.id.setting_change);
        textView = (TextView) ret.findViewById(R.id.water_rate);
        txtBudge = (TextView) ret.findViewById(R.id.show_budge);
        txtAccount = (TextView) ret.findViewById(R.id.water_account);
        changeSet.setOnClickListener(this);
        levelChange();
        return ret;
    }

    /**
     * 为控件添加相应事件
     *
     * @param v
     */
    @Override
    public void onClick (View v) {
        int id = v.getId();
        switch (id) {
            case R.id.setting_change:
                // TODO　自定义Dialog
                EditMoneyDialog dialog = new EditMoneyDialog(
                        getActivity(),
                        R.style.addCategoryDialog
                );
                dialog.setContentView(R.layout.dialog_edit_money);
                Window window = dialog.getWindow();
                window.setGravity(Gravity.CENTER);
                window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                dialog.show();
                dialog.setListener(new EditBudgeInterface() {
                    @Override
                    public void editBudgeMoney (int money) {
                        sumFee = 0;
                        budge = money;
                        result = 0;
                        getSelectedBook();
                        getEditMoney();
                        levelChange();
                        txtBudge.setText("预算：" + money);
                        setEditMoney(money);
                    }
                });
                break;
        }
    }

    /**
     * 结算水平高度变化
     */
    private void levelChange () {
        if (budge <= 0) {
            result = 0;
        } else {
            result = 10 * (budge - sumFee) / budge;
        }

        if (result >= 0 && result <= 2) {
            waveView.setColor(colors[0]);
            startWater(result);
        } else if (result > 2 && result <= 4) {
            waveView.setColor(colors[1]);
            startWater(result);
        } else if (result > 4 && result <= 6) {
            waveView.setColor(colors[2]);
            startWater(result);
        } else if (result > 6 && result <= 8) {
            waveView.setColor(colors[3]);
            startWater(result);
        } else if (result > 8 && result <= 10) {
            waveView.setColor(colors[4]);
            startWater(result);
        }
        txtBudge.setText("预算：" + budge);
        txtAccount.setText("" + (budge - sumFee));
    }

    /**
     * 启动水波
     *
     * @param result
     */
    private void startWater (float result) {
        waveView.setWaterLevel((float) (0.45f + result * 0.03));
        waveView.setAmplitude(10 + result);
        waveView.startWave();
        textView.setText((result * 10) + "%");
    }

    /**
     * 获取选中账本id
     */
    private void getSelectedBook () {
        int bookId = 0;
        Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_ACCOUNT_BOOK, null,
                "ismark=?", new String[] {"1"},
                null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                bookId = cursor.getInt(cursor.getColumnIndex("_id"));
            }
            cursor.close();
        }
        getAllBookDetails(bookId);
    }

    /**
     * 获取选中账本的所有信息
     */
    private void getAllBookDetails (int bookId) {
        if (bookId != 0) {

            Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_ACCOUNT_BOOK_DETAILS, null,
                    "bookid=? and io_choose=?", new String[] {bookId + "", "0"},
                    null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    int fee = cursor.getInt(cursor.getColumnIndex("fee"));
                    sumFee += fee;
                }
                cursor.close();
            }
        } else {
            sumFee = 0f;
        }
    }

    /**
     * 获取设置的月结算金额
     */
    private void getEditMoney () {
        int money = 0;
        Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_EDIT_BUDGE, null,
                "_id=?", new String[] {"1"},
                null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                money = cursor.getInt(cursor.getColumnIndex("edit_money"));
            }
            cursor.close();
        }
        budge = money;
    }

    /**
     * 将设置的月结算金额写入到数据库中
     */
    private void setEditMoney (int money) {
        ContentValues values = new ContentValues();
        values.put("_id", 1);
        values.put("edit_money", money);
        DatabaseUtils.insert(MyConstants.TABLE_EDIT_BUDGE, values);
    }

    @Override
    public void onResume () {
        super.onResume();
        sumFee = 0;
        budge = 0;
        result = 0;
        getSelectedBook();
        getEditMoney();
        levelChange();
    }

    @Override
    public void onDestroy () {
        super.onDestroy();
        sumFee = 0;
        budge = 0;
        result = 0;
    }
}
